<?php

/***************************************************************\
* FILE:		sys/mailfunctions.inc
* SECTION:	pages > mailbox
* FUNCTION:	provide utility functions for mailstuff
* USES:	
* INCLUDES:	sys/inputcheck.inc
* PRODUCES:	
* LAUNCHES:	
\***************************************************************/

function mail_form($contents=array()) {
	$inputs = array();
	
	$inputs[] = form_input_text("Bericht", "body", $contents['body']);
	$inputs[] = form_input_field("Onderwerp", "subject", $contents['subject']);
	$inputs[] = form_input_field("CC:", "cc", $contents['cc']);
	$inputs[] = form_input_field("Aan:", "to", $contents['to']);
	$inputs[] = form_header("Een intern bericht sturen");
	
	$inputs[] = form_input_hidden("mailing", "1");
	
	return form_create("mailform", $inputs, "/service/.mailbox");
}

function mail_send($mail, $keyedaddresses=0) {
	global $CONFIG;
	
	$to_keys = array();
	$cc_keys = array();
	
	$ok = 1;
	
	if (!$keyedaddresses) { // translate addressees to ids
		$nicks = get_keyed_values("user_id", "nick", "MEMBER");
		$nicks = array_map("strtolower", $nicks);
		$nicks = array_flip($nicks);
		$to = split('[,;]', ereg_replace(" ", "", strip_tags($mail['to'])));
		while ($nick = array_pop($to)) {
			$key = $nicks[strtolower($nick)];
			if (!$key) {
				add_alert("'{$nick}' is geen bekende gebruiker");
				$ok = 0;
			} else if ($key == $CONFIG['system_user']) {
				add_alert("de systeemaccount ontvangt geen mail");
				$ok = 0;
			} else
				$to_keys[] = $key;
			unset($key);
		}
		$cc = split('[,;]', ereg_replace(" ", "", strip_tags($mail['cc'])));
		while ($nick = array_pop($cc)) {
			$key = $nicks[strtolower($nick)];
			if ($key === FALSE) {
				add_alert("'{$nick}' is geen bekende gebruiker");
				$ok = 0;
			} else if ($key == $CONFIG['system_user']) {
				add_alert("de systeemaccount ontvangt geen mail");
				$ok = 0;
			} else
				$cc_keys[] = $key;
			unset($key);
		}
	} else {
		$to_keys = split('[,;]', ereg_replace(" ", "", $mail['to']));
		if (count($cc_keys))
			$cc_keys = split('[,;]', ereg_replace(" ", "", $mail['cc']));
	}
	
	include_once('sys/inputcheck.inc');
	
	$ok = (check_exists($mail['subject'], "onderwerp") and $ok) ? 1 : 0;
	$ok = (check_tags($mail['subject'], "onderwerp") and $ok) ? 1 : 0;
	$ok = (check_exists($mail['body'], "tekst") and $ok) ? 1 : 0;
	$ok = (check_tags_advanced($mail['body'], "tekst") and $ok) ? 1 : 0;
	$ok = (check_tags_tables($mail['body'], "tekst") and $ok) ? 1 : 0;
	
	if (!$mail['from'])
		errorpage(9);
	
	if ($ok) {
		db_do_query(db_query_insert(array('sender', 'subject', 'sent', 'hasread', 'icon'), array($mail['from'], db_sqlize($mail['subject']), 'NOW(14)', 1, db_sqlize($mail['icon'])), "MAIL_HEADER"));
		$mail_id = db_insert_id();
		db_do_query(db_query_insert(array('mail_id', 'body'), array($mail_id, db_sqlize($mail['body'])), "MAIL_BODY"));
		$insert = array();
		while ($t = array_pop($to_keys))
			$insert[] = array($t, $mail_id);
		db_do_query(db_query_insert_multiple(array('user_id', 'mail_id'), $insert, "MAIL_TO", 1));
		$insert = array();
		if ($cc_keys and (is_array($cc_keys) and count($cc_keys))) { 
			while ($t = array_pop($cc_keys))
				$insert[] = array($t, $mail_id, 1);
			db_do_query(db_query_insert_multiple(array('user_id', 'mail_id', 'cc'), $insert, "MAIL_TO", 1));
		}
		$_POST['mailing'] = 0;
	} else
		add_alert("Mail werd niet verzonden");
		
}

function fetch_alerts() {
	$alerts = db_do_query(db_query_select(array(array("*")), array("ALERT"), "user_id = {$_SESSION['user_id']}"));
	if (db_num_rows($alerts) > 0)
		while ($a = db_fetch_array($alerts))
			add_alert($a['alert'], '', 0, $a['alert_id']);
}
function fetch_mail() {
	$mails = db_do_query(db_query_select(array(array(), "MAIL_HEADER.*"), 
						 array("MAIL_HEADER INNER JOIN MAIL_TO ON MAIL_HEADER.mail_id = MAIL_TO.mail_id", "DUMMY"), 
						 "MAIL_TO.user_id = {$_SESSION['user_id']} AND sent > '{$_SESSION['member']['last_visit']}' 
						  AND !MAIL_TO.hasread AND !MAIL_TO.trashed AND !MAIL_TO.deleted", "sent DESC"));
	if (db_num_rows($mails) > 0) {
		$m = db_fetch_array($mails);
		add_alert("Nieuw intern bericht !! [<a href=/service/.mailbox>mailbox</a>] - [<a href=/service/{$m[mail_id]}.mail>bericht</a>]");
	}
}

?>
